import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:netease_cloud_music_api/src/album.dart';
import 'package:netease_cloud_music_api/src/artist.dart';
import 'package:netease_cloud_music_api/src/batch.dart';
import 'package:netease_cloud_music_api/src/captcha.dart';
import 'package:netease_cloud_music_api/src/comment.dart';
import 'package:netease_cloud_music_api/src/dj_radio.dart';
import 'package:netease_cloud_music_api/src/login.dart';
import 'package:netease_cloud_music_api/src/message.dart';
import 'package:netease_cloud_music_api/src/mv.dart';
import 'package:netease_cloud_music_api/src/other.dart';
import 'package:netease_cloud_music_api/src/personalized.dart';
import 'package:netease_cloud_music_api/src/playlist.dart';
import 'package:netease_cloud_music_api/src/search.dart';
import 'package:netease_cloud_music_api/src/song.dart';
import 'package:netease_cloud_music_api/src/top.dart';
import 'package:netease_cloud_music_api/src/upload.dart';
import 'package:netease_cloud_music_api/src/user.dart';
import 'package:netease_cloud_music_api/src/video.dart';
import 'package:netease_cloud_music_api/src/yunbei.dart';
import 'package:package_info/package_info.dart';
import 'package:shared_preferences/shared_preferences.dart';


class NeteaseCloudMusicApi with Album, Artist, Batch, Captcha, Comment, DjRadio, Login, Message, Mv, Other, Personalized,
PlayList, Search, Song, Top, Upload, User, Video, YunBei{
  static SharedPreferences? sharedPreferences;
  static const MethodChannel _channel = const MethodChannel('netease_cloud_music_api');

  init () async{
    sharedPreferences = await SharedPreferences.getInstance();
  }

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod('getVersion');
    return version;
  }

  static Future<String> get version async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    final String _version = packageInfo.buildNumber;
    return _version;
  }

  static Map<String, dynamic> getCookieSync({ Map<String, dynamic>? cookie }) {
    if (sharedPreferences == null) {
      return throw('NeteaseCloudMusicApi: 请初始化');
    }
    Map<String, dynamic> _cookie = cookie ?? {};
    String? getCookie = sharedPreferences!.getString('cookie');
    Map<String, dynamic> storageCookie = getCookie == null ? Map<String, dynamic>() : json.decode(getCookie);
    if (storageCookie.isNotEmpty) {
      _cookie.forEach((key, value) {
        storageCookie[key] = value;
      });
    }
    return storageCookie;
  }

  static Future<Map<String, dynamic>> getCookie({ Map<String, dynamic>? cookie }) {
    return SharedPreferences.getInstance().then((sharedPreferences) {
      Map<String, dynamic> _cookie = cookie ?? {};
      String? getCookie = sharedPreferences.getString('cookie');
      Map<String, dynamic> storageCookie = getCookie == null ? Map<String, dynamic>() : json.decode(getCookie);
      if (storageCookie.isNotEmpty) {
        _cookie.forEach((key, value) {
          storageCookie[key] = value;
        });
      }
      return storageCookie;
    });
  }

  static Map<String, dynamic>? setCookieSync({ required Map<String, dynamic> cookie, bool clear = false}) {
    String? getCookie = sharedPreferences!.getString('cookie');
    Map<String, dynamic>? storageCookie = getCookie == null ? Map<String, dynamic>() : json.decode(getCookie);
    Map<String, dynamic>? _cookie = clear ? {} : storageCookie;
    // cookie = cookie ?? {};
    cookie.forEach((key, value) {
      _cookie![key] = value;
    });
    sharedPreferences!.setString('cookie', jsonEncode(_cookie));
    return _cookie;
  }

  static Future<Map<String, dynamic>> setCookie({ required Map<String, dynamic> cookie, bool clear = false}) {
    return SharedPreferences.getInstance().then((sharedPreferences) {
      String? getCookie = sharedPreferences.getString('cookie');
      Map<String, dynamic>? storageCookie = getCookie == null ? Map<String, dynamic>() : json.decode(getCookie);
      Map<String, dynamic> _cookie = clear ? {} : storageCookie!;
      // cookie = cookie ?? {};
      cookie.forEach((key, value) {
        _cookie[key] = value;
      });
      sharedPreferences.setString('cookie', jsonEncode(_cookie));
      return _cookie;
    });
  }

  static bool isLoginSync() {
    return getCookieSync().containsKey('MUSIC_U');
  }

  static Future<bool> isLogin() async {
    Map<String, dynamic> cookie =  await getCookie();
    return cookie.containsKey('MUSIC_U');
  }
}